package com.github.florent37.shapeofview.shapes;


import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.agp.render.BlendMode;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * TriangleView
 *
 * @since 2021-07-26
 */
public class TriangleView extends StackLayout implements Component.EstimateSizeListener, Component.DrawTask {
    private Paint paint = new Paint();
    private int mWidth;
    private int mHeight;
    private float percentBottom = 0.5f;
    private float percentLeft = 0f;
    private float percentRight = 0f;

    /**
     * TriangleView
     *
     * @param context context
     */
    public TriangleView(Context context) {
        super(context);
        init(context, null);
    }

    /**
     * TriangleView
     *
     * @param context context
     * @param attrs attrs
     */
    public TriangleView(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    /**
     * TriangleView
     *
     * @param context context
     * @param attrs attrs
     * @param defStyleAttr defStyleAttr
     */
    public TriangleView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, String.valueOf(defStyleAttr));
        init(context, attrs);
    }

    private void init(Context context, AttrSet attrs) {
        paint.setAntiAlias(true);
        paint.setColor(new Color(Color.getIntColor("#3F51B5")));
        paint.setStyle(Paint.Style.FILL_STYLE);
        if (attrs != null) {
            percentBottom = attrs.getAttr("shape_triangle_percentBottom").get().getFloatValue();
            percentLeft = attrs.getAttr("shape_triangle_percentLeft").get().getFloatValue();
            percentRight = attrs.getAttr("shape_triangle_percentRight").get().getFloatValue();
        }
        setEstimateSizeListener(this::onEstimateSize);
        addDrawTask(this::onDraw);
    }

    @Override
    public boolean onEstimateSize(int width, int height) {
        mWidth = EstimateSpec.getSize(width);
        mHeight = EstimateSpec.getSize(height);
        return false;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        final Path path = new Path();
        path.moveTo(0, percentLeft * mWidth);
        path.lineTo(percentBottom * mWidth, mHeight);
        path.lineTo(mWidth, percentRight * mHeight);
        path.close();
        paint.setBlendMode(BlendMode.SRC_IN);
        canvas.drawPath(path, paint);
        paint.setBlendMode(null);
    }

    /**
     * getPercentBottom
     *
     * @return float
     */
    public float getPercentBottom() {
        return percentBottom;
    }

    /**
     * setPercentBottom
     *
     * @param percentBottom percentBottom
     */
    public void setPercentBottom(float percentBottom) {
        this.percentBottom = percentBottom;
        invalidate();
    }

    /**
     * getPercentLeft
     *
     * @return float
     */
    public float getPercentLeft() {
        return percentLeft;
    }

    /**
     * setPercentLeft
     *
     * @param percentLeft percentLeft
     */
    public void setPercentLeft(float percentLeft) {
        this.percentLeft = percentLeft;
        invalidate();
    }

    /**
     * getPercentRight
     *
     * @return float
     */
    public float getPercentRight() {
        return percentRight;
    }

    /**
     * setPercentRight
     *
     * @param percentRight percentRight
     */
    public void setPercentRight(float percentRight) {
        this.percentRight = percentRight;
        invalidate();
    }


}
